import { createRouter, createWebHistory } from 'vue-router'
import login from '../components/login.vue'
import home from  '../components/home.vue'
import welcome from "../components/welcome.vue";
import users from "../components/User/users.vue";
import roles from "../components/Role/roles.vue";
import rights from "../components/Role/rights.vue";

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      {path: '/welcome', component: welcome},
      {path: '/users', component: users},
      {path: '/roles', component: roles},
      {path: '/rights', component: rights}
    ]
  },

]


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  //ES6新语法，当键跟值是同一个的时候，可以只写键
  routes
})

//设置一下路由守卫

router.beforeEach((to,from,next)=>{
  //登录，不拦截
  if( to.path == '/login' ) return next();
  //先获取token
  let token = sessionStorage.getItem('token');
  //如果没有token，那么进入到login
  if( !token ) return next('/login');

  //如果有，则放行
  next();
})
export default router
